import java.util.*;
public class test {
    // 蓝桥杯 团建(⭐⭐⭐)
    static int n, m;
    static long[] c, d;
    static Map<Integer, Set<Integer>> mp1 = new HashMap<>();
    static Map<Integer, Set<Integer>> mp2 = new HashMap<>();
    static int res = 0;//答案

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        m = scanner.nextInt();
        c = new long[n + 1];
        d = new long[m + 1];
        for (int i = 0; i < n; i++) {
            c[i] = scanner.nextLong();
        }
        for (int i = 0; i < m; i++) {
            d[i] = scanner.nextLong();
        }
        for (int i = 0; i < n - 1; i++) {
            int a = scanner.nextInt();
            int b = scanner.nextInt();
            if (!mp1.containsKey(a)) {
                mp1.put(a, new HashSet<Integer>());
            }
            if (!mp1.containsKey(b)) {
                mp1.put(b, new HashSet<Integer>());
            }
            mp1.get(a).add(b);
            mp1.get(b).add(a);
        }
        for (int i = 0; i < m - 1; i++) {
            int a = scanner.nextInt();
            int b = scanner.nextInt();
            if (!mp2.containsKey(a)) {
                mp2.put(a, new HashSet<Integer>());
            }
            if (!mp2.containsKey(b)) {
                mp2.put(b, new HashSet<Integer>());
            }
            mp2.get(a).add(b);
            mp2.get(b).add(a);
        }
        dfs(0, 0, 1, 1, 1);
        System.out.println(res);
        scanner.close();
    }

    //深度优遍历找最大公共前缀
    public static void dfs(int a, int b, int p, int q, int max) {
        //a、b分别为遍历图一图二上一节点（用于防止回归）
        //p、q分别为遍历图一图二当前节点
        for (int i : mp1.get(p)) {
            for (int j : mp2.get(q)) {
                if (i == a || j == b) continue;
                if (c[i] == d[j]) {
                    res = Math.max(res, max + 1);
                    dfs(p, q, i, j, max + 1);
                }
            }
        }
    }
    // 蓝桥杯 小蓝的图书馆
    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        HashMap<String,Integer> map = new HashMap<>();
        int n = in.nextInt();
        while(n-- > 0){
            String s1 = in.next();
            if(s1.equals("find")){
                String s = in.next();
                System.out.println(map.getOrDefault(s,0));
            }else{
                String s2 = in.next();
                String s3 = in.next();
                map.put(s3,map.getOrDefault(s3,0) + 1);
            }
        }
    }
    // 蓝桥杯 最大数目
    public static void main2(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] arr = new int[n + 1];
        int max = 0;
        HashMap<Integer,Integer> map = new HashMap<>();
        for(int i = 1;i <= n;i++){
            arr[i] = in.nextInt();
            map.put(arr[i], map.getOrDefault(arr[i],0) + 1);
            map.put(arr[i] + 1, map.getOrDefault(arr[i] + 1,0) + 1);
            map.put(arr[i] - 1, map.getOrDefault(arr[i] - 1,0) + 1);
            max = Math.max(max,map.get(arr[i]));
            max = Math.max(max,map.get(arr[i] + 1));
            max = Math.max(max,map.get(arr[i] - 1));
        }
        System.out.println(max);
    }
}
